/**************************************** HY64 startup ****************************************/
/*  This is the templet for HY64 application startup routine work with Bootloader,            */
/*  DO NOT modify this file                                                                   */             
/*                                                                                            */
/*  r66193, Freescale China AutoLab                                                           */
/*  Dec, 16, 2009                                                                             */
/**********************************************************************************************/
#include "system.h"      
#include "sys_config.h"
#include "sys_startup.h"
 

#pragma CODE_SEG BOOT_SEG 

static void ZeroOut(struct _tagStartup *_startupData);
static void CopyDown(struct _tagStartup *_startupData);
void main(void);

typedef void(* APPFn)(void);
APPFn Jump2Main;

/****************************************************************************/
/**
 * Function Name: App
 * Description: none
 *
 * Param:   none
 * Return:  none
 * Author:  2016/04/18, hui.pang create this function
 ****************************************************************************/

/* Stack-pointer change not in debugging-information */
#pragma MESSAGE DISABLE C12053

void App( void ) 
{    
    INIT_SP_FROM_STARTUP_DESC(); 

    ZeroOut( (struct _tagStartup *)&_startupData.flags );
    CopyDown( (struct _tagStartup *)&_startupData.flags );    


	IVBR = 0x40;


    main();    
}

/* Stack-pointer change not in debugging-information */
#pragma MESSAGE WARNING C12053

/****************************************************************************/
/**
 * Function Name: ZeroOut
 * Description: zero out RAM-areas where data is allocated.
 *
 * Param:   none
 * Return:  none
 * Author:  2016/04/18, hui.pang create this function
 ****************************************************************************/
static void ZeroOut( struct _tagStartup *_startupData ) 
{
    int i, j;
    unsigned char *dst;
    _Range *r;

    //////////////////////////////////////
    
    r = _startupData->pZeroOut;
    
    for (i=0; i<_startupData->nofZeroOuts; i++) 
    {
        dst = r->beg;
        j = r->size;
        do {
            *dst = '\0'; // zero out 
            dst++;
            j--;
        } while(j>0);
        r++;
    }
}

/****************************************************************************/
/**
 * Function Name: CopyDown
 * Description: initializes global variables with their values
 *
 * Param:   none
 * Return:  none
 * Author:  2016/04/18, hui.pang create this function
 ****************************************************************************/
static void CopyDown( struct _tagStartup *_startupData ) 
{
    int i;
    unsigned char *dst;
    int *p;

    //////////////////////////////////////////

    p = (int*)_startupData->toCopyDownBeg;                   //1. p=&size
    while (*p != 0) 
    {
        i = *p;                          // nof              //2. i=size
        p++;                                                 //3. p = (int*)(&(unsigned char *)dest)
        dst = (unsigned char*)*p;        // dstAddr          //4. dst = dest
        p++;
        do {                             // p points now into 'bytes' 
            *dst = *((unsigned char*)p); // copy byte-wise 
            dst++;
            ((char*)p)++;
            i--;
        } while (i>0);
    }
}


#pragma CODE_SEG DEFAULT

